﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using YellowMap.Resources;
using System.Windows.Media;

namespace YellowMap.View.Page
{
    public partial class SettingPage : PhoneApplicationPage
    {

        /*
        * [vttrung]: FT04
        */
        public static int searchby = 0;
        public static int searchfrom = 0;
        public SettingPage()
        {
            InitializeComponent();
            ReloadLanguage(AppViewModel.Instance.SettingVM.Language);
            LoadLanguageIndex();
            LoadMapTypeIndex();
            LoadSearchByIndex();
            LoadSearchFromIndex();
            LoadGPSToggle();
            LoadTheme();
        }

        private void LoadGPSToggle()
        {
            if (AppViewModel.Instance.SettingVM.GPS == 0)
            {
                toggle.IsChecked = false;
            }
            else
            {
                toggle.IsChecked = true;
            }
        }

        // Detect Theme of Setting screen and Load theme to app
        private void LoadTheme()
        {
            YellowMap.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            radEn.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            radVn.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            radRoad.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            radAerial.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            radHybrid.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            radTerrain.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            titSettings.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Setting);
            toggle.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            radCurrentLocation.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            radChooseOnMap.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            radSearchBestRated.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            radSearchClosest.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] != Visibility.Visible)
            {
                LayoutRoot.Background = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_screen);
            }
        }

        // Load Language when change language in settings screen
        private void ReloadLanguage(string languageName)
        {
            switch (languageName)
            {
                case "Vi":
                    if (toggle.IsChecked == true)
                    {
                        toggle.Content = LangVi.str_OnGps;
                    }
                    else
                    {
                        toggle.Content = LangVi.str_OffGps;
                    }
                    lbGPS.Text = LangVi.str_GPS;
                    titSettings.Text = LangVi.str_titsettings;
                    lbLanguage.Text = LangVi.str_lbLanguage;
                    lbMaptype.Text = LangVi.str_lbMaptype;
                    radRoad.Content = LangVi.str_RoadMap;
                    radAerial.Content = LangVi.str_Aerial;
                    radHybrid.Content = LangVi.str_Hybrid;
                    radTerrain.Content = LangVi.str_Terrain;
                    lbsearchby.Text = LangVi.str_rank_by;
                    lbsearchfrom.Text = LangVi.str_search_at;
                    radCurrentLocation.Content = LangVi.str_current_location;
                    radChooseOnMap.Content = LangVi.str_choose_on_map;
                    radSearchBestRated.Content = LangVi.str_best_rating;
                    radSearchClosest.Content = LangVi.str_closest;
                    break;
                case "En":
                    if (toggle.IsChecked == true)
                    {
                        toggle.Content = LangEn.str_OnGps;
                    }
                    else
                    {
                        toggle.Content = LangEn.str_OffGps;
                    }
                    lbGPS.Text = LangEn.str_GPS;
                    titSettings.Text = LangEn.str_titsettings;
                    lbLanguage.Text = LangEn.str_lbLanguage;
                    lbMaptype.Text = LangEn.str_lbMaptype;
                    radRoad.Content = LangEn.str_RoadMap;
                    radAerial.Content = LangEn.str_Aerial;
                    radHybrid.Content = LangEn.str_Hybrid;
                    radTerrain.Content = LangEn.str_Terrain;
                    lbsearchby.Text = LangEn.str_rank_by;
                    lbsearchfrom.Text = LangEn.str_search_at;
                    radCurrentLocation.Content = LangEn.str_current_location;
                    radChooseOnMap.Content = LangEn.str_choose_on_map;
                    radSearchBestRated.Content = LangEn.str_best_rating;
                    radSearchClosest.Content = LangEn.str_closest;
                    break;
            }
        }

        // Save and Load Index of Language to keep the user's change
        private void LoadLanguageIndex()
        {
            if (AppViewModel.Instance.SettingVM.Language == "Vi")
            {
                radVn.IsChecked = true;
            }
            else if (AppViewModel.Instance.SettingVM.Language == "En")
            {
                radEn.IsChecked = true;
            }
        }
        private void LoadSearchByIndex()
        {
            if (AppViewModel.Instance.SettingVM.SearchBy == 0)
                radSearchClosest.IsChecked = true;
            else
                radSearchBestRated.IsChecked = true;
        }
        private void LoadSearchFromIndex()
        {
            if (AppViewModel.Instance.SettingVM.SearchFrom == 0)
                radCurrentLocation.IsChecked = true;
            else
                radChooseOnMap.IsChecked = true;
        }
        // Save and Load Index of Map Style to keep the user's change
        private void LoadMapTypeIndex()
        {
            if (AppViewModel.Instance.SettingVM.MapType == 0)
            {
                radRoad.IsChecked = true;
            }
            if (AppViewModel.Instance.SettingVM.MapType == 1)
            {
                radAerial.IsChecked = true;
            }
            else if (AppViewModel.Instance.SettingVM.MapType == 2)
            {
                radHybrid.IsChecked = true;
            }
            else if (AppViewModel.Instance.SettingVM.MapType == 3)
            {
                radTerrain.IsChecked = true;
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ReloadLanguage(AppViewModel.Instance.SettingVM.Language);
            base.OnNavigatedTo(e);
        }

        // Save seletetion of user when choose subcate
        private void radRoad_Checked(object sender, RoutedEventArgs e)
        {
            AppViewModel.Instance.SettingVM.MapType = Constant.MAPTYPE_ROADMAP;
        }
        // Save seletetion of user when choose subcate
        private void radAerial_Checked(object sender, RoutedEventArgs e)
        {
            AppViewModel.Instance.SettingVM.MapType = Constant.MAPTYPE_AERIAL;
        }
        // Save seletetion of user when choose subcate
        private void radHybrid_Checked(object sender, RoutedEventArgs e)
        {
            AppViewModel.Instance.SettingVM.MapType = Constant.MAPTYPE_HYBRID;
        }
        // Save seletetion of user when choose subcate
        private void radTerrain_Checked(object sender, RoutedEventArgs e)
        {
            AppViewModel.Instance.SettingVM.MapType = Constant.MAPTYPE_TERRAIN;
        }
        // Save seletetion of user when choose subcate
        private void radEn_Checked(object sender, RoutedEventArgs e)
        {
            AppViewModel.Instance.SettingVM.Language = Config.str_Eng;
            ReloadLanguage(AppViewModel.Instance.SettingVM.Language);
        }
        // Save seletetion of user when choose subcate
        private void radVn_Checked(object sender, RoutedEventArgs e)
        {
            AppViewModel.Instance.SettingVM.Language = Config.str_Vi;
            ReloadLanguage(AppViewModel.Instance.SettingVM.Language);
        }

        private void radCurrentLocation_Checked(object sender, RoutedEventArgs e)
        {
            AppViewModel.Instance.SettingVM.SearchFrom = Constant.SEARCHFROM_CURRENT;
            AppViewModel.Instance.SettingVM.SearchFrom = 0;
        }

        private void radChooseOnMap_Checked(object sender, RoutedEventArgs e)
        {
            AppViewModel.Instance.SettingVM.SearchFrom = Constant.SEARCHFROM_CHOOSEONMAP;
            AppViewModel.Instance.SettingVM.SearchFrom = 1;
            if (MainPage.Instance.CenterLocation.Latitude != 0 && MainPage.Instance.CenterLocation.Longitude != 0)
            {
                AppViewModel.Instance.SettingVM.MapLattitude = MainPage.Instance.CenterLocation.Latitude;
                AppViewModel.Instance.SettingVM.MapLongitude = MainPage.Instance.CenterLocation.Longitude;
            }
            
        }

        private void radSearchClosest_Checked(object sender, RoutedEventArgs e)
        {
            AppViewModel.Instance.SettingVM.SearchBy = Constant.SEARCHBY_CLOSEST;
            AppViewModel.Instance.SettingVM.SearchBy = 0;
        }

        private void radSearchBestRated_Checked(object sender, RoutedEventArgs e)
        {
            AppViewModel.Instance.SettingVM.SearchBy = Constant.SEARCHBY_BESTRATE;
            AppViewModel.Instance.SettingVM.SearchBy = 1;
        }

        private void toggle_Checked(object sender, RoutedEventArgs e)
        {
            if (AppViewModel.Instance.SettingVM.Language == "Vi")
            {
                toggle.Content = LangVi.str_OnGps;
            }
            else
            {
                toggle.Content = LangEn.str_OnGps;
            }
            AppViewModel.Instance.SettingVM.GPS = 1;
        }

        private void toggle_Unchecked(object sender, RoutedEventArgs e)
        {
            if (AppViewModel.Instance.SettingVM.Language == "Vi")
            {
                toggle.Content = LangVi.str_OffGps;
            }
            else
            {
                toggle.Content = LangEn.str_OffGps;
            }
            AppViewModel.Instance.SettingVM.GPS = 0;
        }

    }
}